<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use think\Log;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Pagearticle extends Backend
{
    
    /**
     * ArticleCate模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('ArticleCate');
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("moduleTypeList", $this->model->getModuleTypeList());
        $this->view->assign("isOpenList", $this->model->getIsOpenList());
        $this->view->assign("isOpen1List", $this->model->getIsOpen1List());
    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = model('ArticleCate')
                ->where($where)->where('type','2')
                ->order($sort, $order)
                ->count();

            $list = model('ArticleCate')
                ->where($where)->where('type','2')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = NULL)
    {

        $row = Db::name('article')->where('site_id',$ids)->find();

        if ($this->request->isPost())
        {

            $this->code = -1;
            $params = $this->request->post("row/a");

            Log::record(json_encode($params));
            Log::record(json_encode($ids));


            if($row) {

                $data = array(
                    'name'=>$params['name'],
                    'content'=>$params['content'],
                );
                Db::name('article')->where('site_id',$ids)->update($data);

            }else{
                $data = array(
                    'name'=>$params['name'],
                    'content'=>$params['content'],
                    'site_id'=>$ids,
                    'publish'=>date('Y-m-d H:i:s'),
                    'addtime'=>time(),
                    'status'=>1

                );
                    Db::name('article')->insert($data);

            }

            $this->code = 1;
            return ;

        }

        if (!$row){

            $row = array(
                'name'=>'',
                'littitle'=>'',
                'jumpurl'=>'',
                'content'=>'',
                'author'=>'',
                'weigh'=>'99',
                'site_id'=>$ids,
            );
            $this->view->assign("row", $row);
            return $this->view->fetch('edit');
        }



        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
